// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Eggman Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 71 : TYPE_EGGMAN

// Function declarations
reserve function Eggman_Function163
reserve function Eggman_Function164
reserve function BossGotMugged

// Static Values

// Tables
public table Eggman_table36
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0
end table

function BossGotMugged
	temp0 = 8400
	temp0 <<= 16
	if object[0].xpos > temp0
		if object.state < 7
			object.state = 7
		end if
	end if
end function

function Eggman_Function163
	Sin256(temp0, object.value3)
	temp0 <<= 10
	object.ypos += temp0
	object.value3 += 2
	object.value3 &= 255
end function


function Eggman_Function164
	object.value0 += object.xvel
	object.value1 += object.yvel
	object.xpos = object.value0
	object.ypos = object.value1
end function


event ObjectMain
	switch object.state
	case 0
		GetTableValue(temp0, 0, Eggman_table36)
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object[currentPlayer].xpos >= temp0
				object.xvel = 0x6000
				object.yvel = -0x18000
				object.value1 = object.ypos
				object.value3 = 0
				object.state++
				object.priority = PRIORITY_ACTIVE
			end if
		next
		CallFunction(Eggman_Function164)
		if object.state == 0
			CallFunction(Eggman_Function163)
		end if
		break
	case 1
		temp1 = -2
		GetTableValue(temp0, 1, Eggman_table36)
		if object.value0 >= temp0
			object.value0 = temp0
			object.xvel = 0
			temp1++
		end if
		GetTableValue(temp0, 2, Eggman_table36)
		if object.value1 <= temp0
			object.value1 = temp0
			object.yvel = 0
			temp1++
		end if
		if temp1 == 0
			object.xvel = 0x14000
			object.yvel = -0x20000
			object.state++
		end if
		CallFunction(Eggman_Function164)
		break
	case 2
		temp1 = -2
		GetTableValue(temp0, 3, Eggman_table36)
		if object.value0 >= temp0
			object.value0 = temp0
			object.xvel = 0
			temp1++
		end if
		GetTableValue(temp0, 4, Eggman_table36)
		if object.value1 <= temp0
			object.value1 = temp0
			object.yvel = 0
			temp1++
		end if
		if temp1 == 0
			object.yvel = -0x18000
			object.state++
		end if
		CallFunction(Eggman_Function164)
		break
	case 3
		GetTableValue(temp0, 5, Eggman_table36)
		if object.value1 <= temp0
			object.value1 = temp0
			if object.value2 == 0
				object.xvel = 0x14000
				object.yvel--
			else
				object.xvel = 0x28000
				object.yvel--
			end if
			object.direction = FLIP_X
			object.value0 = object.xpos
			object.state++
			CallFunction(Eggman_Function164)
		else
			object.value4 += 2
			object.value4 &= 255
			Cos256(temp0, object.value4)
			if temp0 > 0
				object.direction = FLIP_X
			else
				object.direction = FLIP_NONE
			end if
			Sin256(object.xpos, object.value4)
			object.xpos <<= 12
			object.xpos += object.value0
			temp0 = object.yvel
			temp1 = 0x7FFFFFFF
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				if object[currentPlayer].ypos < temp1
					temp1 = object[currentPlayer].ypos
				end if
			next
			temp1 -= object.value1
			if temp1 >= 0
				temp1 -= 0x480000
				if temp1 >= 0
					temp0 >>= 1
					temp1 -= 0x280000
					if temp1 >= 0
						temp0 >>= 1
						temp1 -= 0x280000
						if temp1 >= 0
							temp0 = 0
						end if
					end if
				end if
			end if
			if object.value2 == 1
				temp0 <<= 1
			end if
			if temp0 == 0
				object.ypos = object.value1
				CallFunction(Eggman_Function163)
			else
				object.value1 = object.ypos
				object.value3 = 0
				object.value1 += temp0
				object.ypos = object.value1
			end if
		end if
		break
	case 4
		temp1 = -2
		GetTableValue(temp0, 6, Eggman_table36)
		if object.value0 >= temp0
			object.value0 = temp0
			object.xvel = 0
			temp1++
		end if
		GetTableValue(temp0, 7, Eggman_table36)
		if object.value1 <= temp0
			object.value1 = temp0
			object.yvel = 0
			temp1++
		end if
		if temp1 == 0
			if object.value2 == 0
				object.direction = FLIP_NONE
			end if
			object.value3 = 0
			object.state++
		end if
		CallFunction(Eggman_Function164)
		break
	case 5
		temp2 = 0
		if object.value2 == 0
			GetTableValue(temp0, 8, Eggman_table36)
			GetTableValue(temp1, 9, Eggman_table36)
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				if object[currentPlayer].xpos >= temp0
					if object[currentPlayer].ypos <= temp1
						temp2 = 1
						object.value5 = 50
						object.animation = 4
						object.animationTimer = 32
					end if
				end if
			next
		else
			temp2 = 1
			object.value5 = 1
		end if
		if temp2 == 1
			object.state++
		end if
		CallFunction(Eggman_Function164)
		CallFunction(Eggman_Function163)
		break
	case 6
		object.value5--
		if object.value5 == 0
			stage.newWaterLevel = 0xBB80000
			object.value5 = 0
			object.xvel = 0x4000
			object.yvel = -0x40000
			object.priority = PRIORITY_ACTIVE
			object.animation = 6
			object.animationTimer = 0
			object.value6 = 2
			object.value1 = object.ypos
			object.state++
			if stage.debugMode == 0
				if object.value10 == 0
					achievement.trigger = 7
					//CallNativeFunction2(SetAchievement, 6, 100)
				end if
			end if
		else
			if object.value5 == 24
				object.animation = 1
				object.animationTimer = 0
				object.direction = FLIP_X
			end if
		end if
		CallFunction(Eggman_Function164)
		if object.state == 6
			CallFunction(Eggman_Function163)
		end if
		break
	case 7
		stage.newWaterLevel = 0xBB80000
		if object.outOfBounds == 1
			ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, 0, 0)
			object.value2 = 0
		else
			CallFunction(Eggman_Function164)
		end if
		break
	end switch
	CallFunction(BossGotMugged)
	if object.value8 != 0
		if object.value9 > 0
			object.value9--
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.value9 == 0
				CheckEqual(object[currentPlayer].animation, ANI_HURT)
				temp0 = checkResult
				CheckEqual(object[currentPlayer].animation, ANI_DYING)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].animation, ANI_DROWNING)
				temp0 |= checkResult
				if temp0 != 0
					if object.animation != 2
						object.animation = 2
						object.animationTimer = 0
					end if
					if currentPlayer == 0
						object.value10 = 1
					end if
				end if
				BoxCollisionTest(C_TOUCH, object.entityPos, -23, -16, 23, 16, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
				if checkResult == 0
					BoxCollisionTest(C_TOUCH, object.entityPos, -16, -21, 16, 21, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
				end if	
				if object.state == 7
					checkResult = 0
				else
					if checkResult == 1
						CallFunction(PlayerObject_CheckHit)
					else
						CallFunction(BossTarget)
					end if
				end if	
				if checkResult == 1
					object.value8--
					if object.value8 == 0
						player.score += 1000
						object.animation = 4
						object.animationTimer = 0
						object.value2 = 1
					else
						object.animation = 3
						object.animationTimer = 0
						object.value9 = 32
						PlaySfx(SfxName[Boss Hit], 0)
					end if
				end if
			end if
		next

		foreach (TypeName[Bullet], arrayPos1, ACTIVE_ENTITIES)
			if object.value9 == 0
				BoxCollisionTest(C_TOUCH, object.entityPos, -23, -16, 23, 16, arrayPos1, -6, -4, 6, 4)	
				if checkResult == 0
					BoxCollisionTest(C_TOUCH, object.entityPos, -16, -21, 16, 21, arrayPos1, -6, -4, 6, 4)	
				end if	
				if object.state == 7
					checkResult = 0
				end if	
				if checkResult == 1
					object.value8--
					if object.value8 == 0
						player.score += 1000
						object.animation = 4
						object.animationTimer = 0
						object.value2 = 1
					else
						object.animation = 3
						object.animationTimer = 0
						object.value9 = 32
						PlaySfx(SfxName[Boss Hit], 0)
					end if
				end if
			end if
		next

		if stage.playerListPos == 6
			if saved.gogoplus > 0
				temp0 = 0
				temp1 = object.xpos
				temp1 >>= 16
				temp1 -= screen.xoffset
				temp1 += 0x20
				temp2 = object.ypos
				temp2 >>= 16
				temp2 -= screen.yoffset
				temp2 += 0x20
				temp3 = 0
				temp4 = screen.xsize
				temp4 += 0x40
				temp5 = screen.ysize
				temp5 += 0x40
				
				
				if temp1 >= 0
					if temp1 <= temp4
						if temp2 >= 0
							if temp2 <= temp5
								temp3 = 1
							end if
						end if
					end if
				end if
				
				if temp3 == 1
					foreach (TypeName[Insta Shield], arrayPos0, ALL_ENTITIES)
						switch object[arrayPos0].propertyValue
							case 1
							case 3
								temp0 = 1
								break
							case 2
								if arrayPos0 == 13
									if object[arrayPos0].state < 3
										CheckEqual(object.entityPos, object[13].value15)
										temp3 = checkResult
										CheckEqual(object.entityPos, object[14].value15)
										temp3 |= checkResult
										CheckEqual(object.entityPos, object[15].value15)
										temp3 |= checkResult
										CheckEqual(object.entityPos, object[16].value15)
										temp3 |= checkResult
										
										if temp3 == 0
											temp0 = 0
											temp2 = 0
											while temp0 < 22
												if temp2 == 0
													GetTableValue(temp1, temp0, Target_Table_Pos)
													if temp1 == object.entityPos
														temp4 = object.xpos
														temp4 -= object[0].xpos
														Abs(temp4)
														temp4 >>= 19
														temp4 *= temp4
														
														temp5 = object.ypos
														temp5 -= object[0].ypos
														Abs(temp5)
														temp5 >>= 19
														temp5 *= temp5
														temp4 += temp5
														
														SetTableValue(temp4, temp0, Target_Table_Dist)
														temp2 = 1
													end if
												end if	
											temp0++
											loop
											
											if temp2 == 0
												temp4 = object.xpos
												temp4 -= object[0].xpos
												Abs(temp4)
												temp4 >>= 19
												temp4 *= temp4
												
												temp5 = object.ypos
												temp5 -= object[0].ypos
												Abs(temp5)
												temp5 >>= 19
												temp5 *= temp5
												temp4 += temp5
														
												SetTableValue(temp4, 21, Target_Table_Dist)
												SetTableValue(object.entityPos, 21, Target_Table_Pos)
											end if
											
											temp0 = 0
											while temp0 < 22
												temp1 = temp0
												temp1++
												while temp1 < 22
													GetTableValue(temp6, temp0, Target_Table_Dist)
													GetTableValue(temp7, temp1, Target_Table_Dist)
													CheckGreater(temp6, temp7)
													temp4 = checkResult
													CheckEqual(temp6, 0)
													temp4 |= checkResult
													if temp4 == 1
														temp5 = temp6
														temp6 = temp7
														temp7 = temp5
														SetTableValue(temp6, temp0, Target_Table_Dist)
														SetTableValue(temp7, temp1, Target_Table_Dist)
														GetTableValue(temp6, temp0, Target_Table_Pos)
														GetTableValue(temp7, temp1, Target_Table_Pos)
														temp5 = temp6
														temp6 = temp7
														temp7 = temp5
														SetTableValue(temp6, temp0, Target_Table_Pos)
														SetTableValue(temp7, temp1, Target_Table_Pos)
													end if			
													temp1++
												loop
												temp0++
											loop
										end if
									end if
								end if
								if object[arrayPos0].state == 2
									if object[arrayPos0].value15 == object.entityPos
										BoxCollisionTest(C_TOUCH, arrayPos0, -8, -8, 8, 8, object.entityPos, -8, -8, 8, 8)
										if checkResult == 1
											arrayPos1 = arrayPos0
											arrayPos1++
											if arrayPos1 > 16
												arrayPos1 = 13
											end if
											object[arrayPos0].state = 0
											object[arrayPos0].value15 = 0
											temp0 = 1
										end if
									end if
								end if
								break
						end switch
					next
				else
					temp0 = 0	
				end if
			else
				temp0 = 0
			end if	
			
			if temp0 == 1
				object.value8 -= 2
				if object.value8 == 0
					player.score += 1000
					object.animation = 4
					object.animationTimer = 0
					object.value2 = 1
				else
					object.animation = 3
					object.animationTimer = 0
					object.value9 = 32
					PlaySfx(SfxName[Boss Hit], 0)
				end if
			end if
		end if
	end if
	if object.value2 == 1
		temp0 = oscillation
		temp0 &= 7
		if temp0 == 0
			Rand(temp0, 96)
			temp0 -= 48
			temp0 <<= 16
			temp0 += object.xpos
			Rand(temp1, 48)
			temp1 -= 24
			temp1 <<= 16
			temp1 += object.ypos
			CreateTempObject(TypeName[Explosion], 0, temp0, temp1)
			PlaySfx(SfxName[Explosion], 0)
		end if
	end if
end event


event ObjectDraw
	switch object.animation
	case 1
		temp0 = object.animationTimer
		temp0 /= 6
		object.animationTimer++
		object.animationTimer %= 12
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		temp0 = object.animationTimer
		temp0 %= 10
		temp0 /= 5
		temp0 += 9
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			if object[0].animation != ANI_DYING
				object.animation = 1
			end if
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 3
		temp0 = object.animationTimer
		temp0 &= 1
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 30
			object.animationTimer = 0
			object.animation = 1
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 4
		temp0 = object.animationTimer
		temp0 >>= 5
		temp0 += 4
		object.animationTimer++
		object.animationTimer &= 63
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 5
		DrawSpriteFX(6, FX_FLIP, object.xpos, object.ypos)
		break
	case 6
		temp0 = object.animationTimer
		temp0 >>= 2
		temp0 += 7
		object.animationTimer++
		object.animationTimer %= 7
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	end switch
	switch object.value6
	case 1
		temp0 = object.value7
		temp0 >>= 2
		temp0 += 11
		object.value7++
		object.value7 &= 7
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		temp0 = object.value7
		temp0 /= 3
		temp0 += 11
		object.value7++
		if object.value7 > 21
			object.value7 = 18
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	end switch
end event


event ObjectStartup
	if stage.playerListPos == 2
		if saveRAM[279] == 1
			LoadSpriteSheet("Global/Eggrobo.gif")
		else
			LoadSpriteSheet("Global/Eggman.gif")
		end if
	else
		LoadSpriteSheet("Global/Eggman.gif")
	end if
	SpriteFrame(-28, -32, 64, 56, 1, 1)
	SpriteFrame(-28, -32, 64, 56, 66, 1)
	SpriteFrame(-28, -32, 64, 56, 131, 1)
	SpriteFrame(-28, -32, 64, 56, 1, 58)
	SpriteFrame(-28, -32, 64, 56, 1, 1)
	SpriteFrame(-28, -32, 64, 56, 1, 58)
	SpriteFrame(-28, -32, 64, 56, 66, 58)
	SpriteFrame(-28, -32, 64, 56, 131, 58)
	SpriteFrame(-28, -32, 64, 56, 66, 1)
	SpriteFrame(-28, -32, 64, 56, 1, 115)
	SpriteFrame(-28, -32, 64, 56, 66, 115)
	SpriteFrame(34, 1, 10, 9, 196, 2)
	SpriteFrame(33, 0, 15, 11, 196, 12)
	SpriteFrame(34, -1, 22, 14, 213, 1)
	SpriteFrame(34, -8, 32, 26, 213, 16)
	SpriteFrame(34, -1, 22, 14, 213, 1)
	SpriteFrame(34, -8, 32, 26, 213, 16)
	SpriteFrame(34, 1, 10, 9, 196, 2)
	SpriteFrame(33, 0, 15, 11, 196, 12)
	foreach (TypeName[LZ Eggman], arrayPos0, ALL_ENTITIES)
			temp0 = -0x700000
			temp0 += object[arrayPos0].xpos
			SetTableValue(temp0, 0, Eggman_table36)
			temp0 = 0x380000
			temp0 += object[arrayPos0].xpos
			SetTableValue(temp0, 1, Eggman_table36)
			temp0 = -0xC00000
			temp0 += object[arrayPos0].ypos
			SetTableValue(temp0, 2, Eggman_table36)
			temp0 = 0x600000
			temp0 += object[arrayPos0].xpos
			SetTableValue(temp0, 3, Eggman_table36)
			temp0 = -0x1000000
			temp0 += object[arrayPos0].ypos
			SetTableValue(temp0, 4, Eggman_table36)
			temp0 = -0x4C00000
			temp0 += object[arrayPos0].ypos
			SetTableValue(temp0, 5, Eggman_table36)
			temp0 = 0x13C0000
			temp0 += object[arrayPos0].xpos
			SetTableValue(temp0, 6, Eggman_table36)
			temp0 = -0x5000000
			temp0 += object[arrayPos0].ypos
			SetTableValue(temp0, 7, Eggman_table36)
			temp0 = 0xB80000
			temp0 += object[arrayPos0].xpos
			SetTableValue(temp0, 8, Eggman_table36)
			temp0 = -0x4D00000
			temp0 += object[arrayPos0].ypos
			SetTableValue(temp0, 9, Eggman_table36)
			object[arrayPos0].value0 = object[arrayPos0].xpos
			object[arrayPos0].value1 = object[arrayPos0].ypos
			object[arrayPos0].animation = 1
			object[arrayPos0].value8 = 8
	next
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
